$(function(){
	
	var urlFull=window.location.href;
	var urlArr=urlFull.split("/");
	proName=urlArr[3];
	proName="/"+proName;
	
	//构建用户详细列表
	$("#gridUserDetail").datagrid({
	    width: "100%",
	    height: 'auto',
	    url: proName+"/user/detail/list?random="+Math.random(),
	    method: 'get',
	    sortName: 'title',
	    loadMsg: "数据加载中...",
	    pagination:true,
	    pageSize: 19,//每页显示的记录条数，默认为10
	    pageList:[19,20,30],//每页显示几条记录
	    striped: true,
	    singleSelect:true,
	    toolbar:"#tbUserDetail"
	});
	
	//是否主页
	var mainframe=$("#mainframe").val();
	
	if(mainframe==="1")
	{
		//未给出诊断患者
		$("#gridUserDetailNoZD").datagrid({
		    width: "100%",
		    height: 'auto',
		    method: 'get',
		    sortName: 'title',
		    pagination:true,
		    pageSize: 18,
		    pageList:[18,20,30],
		    striped: true,
		    singleSelect:true
		});
		
		//未填写临床观察表患者
		$("#gridUserObserveNo").datagrid({
		    width: "100%",
		    height: 'auto',
		    url: proName+"/user/detail/list?observe=1",
		    method: 'get',
		    sortName: 'title',
		    pagination:true,
		    pageSize: 18,
		    pageList:[18,20,30],
		    striped: true,
		    singleSelect:true
		});
		
		//30秒刷新一下用户信息列表
		setInterval("relashUserDetailGrid()",30000);
		selectMainframeByDate(0);
	}
	
});

//定时刷新用户信息表
function relashUserDetailGrid()
{
	//查询条件选择今天的时候才自动刷新
	var qd48Val=$("input[name='qd48']").val();
	if(qd48Val==0)
	{
		selectMainframeByDate(0);
	}
}

//统计未提交病史患者
function countUserDetailNoBS(stageDateStart,stageDateEnd)
{
	$.ajax({
		type: "get",
		url: proName+"/user/detail/count",
		data:{
			stage:'1',
			stageDateStart:stageDateStart,
			stageDateEnd:stageDateEnd
		},
		success: function(res){
			$("#countUserDetailNoBS").html(res);
		}
	});
}

//统计未提交检查结果患者
function countUserDetailNoFJ(stageDateStart,stageDateEnd)
{
	$.ajax({
		type: "get",
		url: proName+"/user/detail/count",
		data:{
			stage:'2',
			stageDateStart:stageDateStart,
			stageDateEnd:stageDateEnd
		},
		success: function(res){
			$("#countUserDetailNoFJ").html(res);
		}
	});
}

//统计未给出诊断患者
function countUserDetailNoZD(stageDateStart,stageDateEnd)
{
	$.ajax({
		type: "get",
		url: proName+"/user/detail/count",
		data:{
			stage:'3',
			stageDateStart:stageDateStart,
			stageDateEnd:stageDateEnd
		},
		success: function(res){
			$("#countUserDetailNoZD").html(res);
		}
	});
}

//统计今日已给出诊断患者
function countUserDetailZDTody(stageDateStart,stageDateEnd)
{
	$.ajax({
		type: "get",
		url: proName+"/user/detail/count",
		data:{
			stage:'4',
			stageDateStart:stageDateStart,
			stageDateEnd:stageDateEnd
		},
		success: function(res){
			$("#countUserDetailZDTody").html(res);
		}
	});
}

//统计全部未填写观察表患者
function countUserObserveNo()
{
	$.ajax({
		type: "get",
		url: proName+"/user/detail/count",
		data:{
			observe:'1',
		},
		success: function(res){
			$("#countUserObserveNo").html(res);
		}
	});
}

/**
 * 构建操作列
 * val: the field value，也就是field:'id'。
 * row: the row record data。就是这一行的Json数据，包括你已经选择在Datagrid上显示的内容，和没显示的内容。
 * index:索引号
 */
function optColumn(val,row,index)
{
	var questionnaireId=row.questionnaireId;
	var diagnosisStr=$.ajax({
        async : false,  
        cache : true,
        type : 'post',  
        url : proName+'/questionnaire/diagnosis/get',  
        data : {  
            'openId' : row.openId,
            'questionnaireId':questionnaireId
        }
    }).responseText;  
	var diagnosisObj=JSON.parse(diagnosisStr); 
	
	var url="'"+proName+"/questionnaire/gotoPage?pageName=answer_questionnaire_48&questionnaireId="+questionnaireId+"&answer_id="+diagnosisObj.id+"'";
	$("#div_btn_edit_answer").show();
	if(questionnaireId!="48")
	{
		$("#div_btn_edit_answer").hide();
		url="'"+proName+"/questionnaire/answer/view?questionnaireId="+questionnaireId+"&answerId="+diagnosisObj.id+"'";
	}
	var optStr="<a href=\"javascript:openWinQuestionnaireDiagnosis("+url+",'"+diagnosisObj.id+"')\"  style='color:blue'><u>诊断</u></a>";
	return optStr;
}

/**
 * 构建未填写临床观察表患者列表操作列
 * val: the field value，也就是field:'id'。
 * row: the row record data。就是这一行的Json数据，包括你已经选择在Datagrid上显示的内容，和没显示的内容。
 * index:索引号
 */
function optColumnObserve(val,row,index)
{
	var observe=row.observe;
	var optStrObserve="";
	if(observe==="1")
	{
		optStrObserve+="<a href='javascript:openWinObserve(\""+row.name+"\",\""+row.wxOpenid+"\")' style='color:blue'>手术记录</a>";
	}
	
	return optStrObserve;
}

/**
 * 用户详细页构建操作列
 * @param val
 * @param row
 * @param index
 */
function optColumnDetail(val,row,index)
{
	var urlStr="../questionnaire/gotoPage?pageName=questionnaire54&userName="+row.name+"&openId="+row.openId+"&btnShow=true";
	var optStr="";
	var observe=row.observe;
	if(observe==="1")
	{
		optStr+="<a href='"+urlStr+"' style='color:blue'>手术记录</a>";
	}
	
	return optStr;
}

/**
 * 构建户籍类型列
 * @param val
 * @param row
 * @param index
 */
function householdTypeColumn(val,row,index)
{
	var householdTypeStr="";
	
	switch(Number(val))
	{
	case 1:
		householdTypeStr="城镇户口";
		break;
	case 2:
		householdTypeStr="农村户口";
		break;
	}
	
	return householdTypeStr;
}

/**
 * 构建婚姻状态列
 * @param val
 * @param row
 * @param index
 */
function marryColumn(val,row,index)
{
	var marryStr="";
	
	switch(Number(val))
	{
	case 0:
		marryStr="已婚";
		break;
	case 1:
		marryStr="未婚";
		break;
	case 2:
		marryStr="离异";
		break;
	case 3:
		marryStr="丧偶";
		break;
	}
	
	return marryStr;
}

/**
 * 构建文化程度列
 * @param val
 * @param row
 * @param index
 */
function educationColumn(val,row,index)
{
	var educationStr="";
	
	switch(Number(val))
	{
	case 1:
		educationStr="小学";
		break;
	case 2:
		educationStr="初中";
		break;
	case 3:
		educationStr="高中";
		break;
	case 4:
		educationStr="大专";
		break;
	case 5:
		educationStr="本科";
		break;
	case 6:
		educationStr="硕士";
		break;
	case 7:
		educationStr="博士";
		break;
	}
	
	return educationStr;
}

//临床观察表列
function observeColumn(val,row,index)
{
	var observeStr="";
	switch(Number(val))
	{
	case 0:
		observeStr="暂不填";
		break;
	case 1:
		observeStr="待填";
		break;
	case 2:
		observeStr="已填";
		break;
	}
	return observeStr;
}

//时间条件查询工作台列表、总览
function selectMainframeByDate(qd48Val)
{
	$("span[name='spaSelDate']").attr("class","btnDate");
	$("#spaSelDate"+qd48Val).attr("class","btnDateSel");
	
	$("#txtQd48").val(qd48Val);
	
	var todayD=new Date();
	todayD=todayD.addDate(qd48Val);
	var todayE=todayD.format("yyyy-MM-dd");
	var stageDateStart=todayE+" 00:00:00";
	var stageDateEnd=todayE+" 23:59:59";
	
	$('#gridUserDetailNoZD').datagrid({
		 url: proName+"/user/detail/list?stage=3"
	});
	
	$('#gridUserDetailNoZD').datagrid('load',{
		stageDateStart:stageDateStart,
		stageDateEnd:stageDateEnd
	});
	
	countUserDetailNoBS(stageDateStart,stageDateEnd);
	countUserDetailNoFJ(stageDateStart,stageDateEnd);
	countUserDetailNoZD(stageDateStart,stageDateEnd);
	countUserDetailZDTody(stageDateStart,stageDateEnd);
	gridUserObserveNoReload();
}

//查询用户详细列表
function searchUserDetail()
{
	var name=$("#txtSearchName").textbox("getValue");
	var mobilePhone=$("#txtSearchMobilePhone").textbox("getValue");
	var observe=$("#selSearchObserve").combobox("getValue");
	var repeatAbort=$("#selRepeatAbort").combobox("getValue");
	
	$('#gridUserDetail').datagrid('load',{
		name:name,
		mobilePhone:mobilePhone,
		observe:observe,
		repeatAbort:repeatAbort
	});
}

/**
 * 打开服务对象临床观察表对话框
 * @param userName 患者姓名
 * @param wxOpenid 微信openid
 */
function openWinObserve(userName,wxOpenid)
{
	var url="../questionnaire/gotoPage?pageName=questionnaire54&userName="+userName+"&openId="+wxOpenid+"&btnShow=false";
	var conStr='<iframe id="iframeQuestionnaire54" style="width:100%;height:100%;border:0;" src="'+url+'"></iframe>';
	$('#winObserveView').html(conStr);
	var winWidth=$(document).width();
	var winHeight=$(document).height();
	var title="【"+userName+"】临床观察表";
	$('#winObserve').window({
		title:title,
	    width:winWidth,
	    height:winHeight,
	    left:0,
	    top:0,
	    modal:true   
	}); 
	$('#winObserve').window('open'); 
	
}

//提交临床观察表
function submitObserve()
{
	var ifr = document.getElementById('iframeQuestionnaire54');
	var iframeQuestionnaire54 = ifr.window || ifr.contentWindow;
	iframeQuestionnaire54.submitFrm();
}

//关闭临床观察表对话框窗口
function closeWinObserve()
{
	$("#winObserve").window('close');  
}

//重新未填写临床观察表的患者列表
function gridUserObserveNoReload()
{
	countUserObserveNo();
	$("#gridUserObserveNo").datagrid("reload");
}

